package bairui.international.console.web.controller.branch.branch.staff;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.QTB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.status.State_User;
import bairui.international.service.Service_BranchCompanyStaff;

/**
 *
 * 详情
 *
 * @generated
 * 
 * @see STest_C_BranchCompanyStaff_Detail_Page 测试
 *
 */
@Controller
public class C_BranchCompanyStaff_Detail_Page
 extends SC_BranchCompanyStaff_Detail_Page 
{
	
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.BranchId) long branchId, 
		@PathVariable(name=Page.PathVariableName.Id) long id
	) throws Exception {
		org.springframework.data.domain.Page<TB_BranchCompanyStaff> page = service_BranchCompanyStaff.page(
				it->filter(it, id, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		return Page.Output.output(
				page
					.map(it->new Page.Output.Model(it){
							@Override protected void assign_before() {
							}
							@Override public String assignAvatarUrl() {
								return it.getUser().getAvatarUrl();
							}
							@Override public String assignNickName() {
								return it.getUser().getNickName();
							}
							@Override public String assignRealName() {
								return it.getUser().getRealName();
							}
							@Override public String assignPhone() {
								return it.getUser().getPhone();
							}
							@Override public java.util.Date assignCreateTime() {
								return it.getUser().getCreateTime();
							}
							@Override public State_User assignState() {
								return it.getUser().getState();
							}
						}
					)
			);
	}
	
	/*
	 * 过滤
	 */
	static BooleanBuilder filter(QTB_BranchCompanyStaff it, long id, HttpServletRequest request){
		return Page.Filter.query(it, request, new Page.Filter.FilterExtends(){
				@Override public Predicate gainNickName(String nickName) {
					return it.user.nickName.like("%" + nickName + "%");
				}
				@Override public Predicate gainRealName(String realName) {
					return it.user.realName.like("%" + realName + "%");
				}
				@Override public Predicate gainPhone(String phone) {
					return it.user.phone.like("%" + phone + "%");
				}
				@Override public Predicate gainState(State_User state) {
					return it.user.state.eq(state);
				}
				@Override public Predicate gainCreateTime_start(Date createTime) {
					return it.user.createTime.after(createTime);
				}
				@Override public Predicate gainCreateTime_end(Date createTime) {
					return it.user.createTime.before(createTime);
				}
			})
			.and(it.prev.id.eq(id));
	}
	
	/*
	 * 排序
	 */
	static Sort sort(QTB_BranchCompanyStaff it, HttpServletRequest request){
		return Page.Output.sort(it, request, new Page.Output.OrderExtends(){
				@Override public Sort gainAvatarUrl(Direction direction) {
					return sort(direction, it.user.avatarUrl);
				}
				@Override public Sort gainNickName(Direction direction) {
					return sort(direction, it.user.nickName);
				}
				@Override public Sort gainRealName(Direction direction) {
					return sort(direction, it.user.realName);
				}
				@Override public Sort gainPhone(Direction direction) {
					return sort(direction, it.user.phone);
				}
				@Override public Sort gainCreateTime(Direction direction) {
					return sort(direction, it.user.createTime);
				}
				@Override public Sort gainState(Direction direction) {
					return sort(direction, it.user.state);
				}
			});
	}
	
	

}
